package net.basshista.jsshtunneling.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import net.basshista.jsshtunneling.model.Host;

public class HostForm extends JPanel {

    private static ResourceBundle MESSAGES = ResourceBundle.getBundle("messages");
    public static final String USER = "user";
    public static final String PASS = "pass";
    public static final String HOST = "host";
    public static final String PORT = "port";
    public static final String OK = "ok";
    public static final String TEST = "test";

    private Host host;

    /**
     * 
     */
    private static final long serialVersionUID = 6604472848200447528L;

    private JLabel lbHost, lbUser, lbPass, lbPort;
    private JTextField tfHost, tfUser, tfPass, tfPort;
    private JButton btTest, btOK;

    public HostForm() {
	host = new Host();
	init();
    }

    private void init() {

	tfHost = new JTextField();
	tfUser = new JTextField();
	tfPass = new JPasswordField();
	tfPort = new JNumberField();

	lbHost = new JLabel(MESSAGES.getString(HOST));
	lbUser = new JLabel(MESSAGES.getString(USER));
	lbPass = new JLabel(MESSAGES.getString(PASS));
	lbPort = new JLabel(MESSAGES.getString(PORT));

	lbHost.setLabelFor(tfHost);
	lbUser.setLabelFor(tfUser);
	lbPass.setLabelFor(tfPass);
	lbPort.setLabelFor(tfPort);

	btOK = new JButton(MESSAGES.getString(OK));
	btOK.setEnabled(false);
	btOK.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {

	    }
	});

	btTest = new JButton(MESSAGES.getString(TEST));
	btTest.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		btOK.setEnabled(host.validate(tfHost.getText(), Integer.parseInt(tfPort.getText()), tfUser.getText(),
			tfPass.getText()));
	    }
	});

	this.setLayout(new SpringLayout());

	this.add(lbHost);
	this.add(tfHost);
	this.add(lbUser);
	this.add(tfUser);
	this.add(lbPass);
	this.add(tfPass);
	this.add(lbPort);
	this.add(tfPort);
	this.add(btTest);
	this.add(btOK);

	SpringLayoutUtilities.makeCompactGrid(this, 5, 2, 5, 5, 5, 5);
    }

}